﻿using Janus.Cores.Repositories;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;
using System.IO;
using System.Xml;

namespace Janus.Mes.ORM.iBatis
{
    /// <summary>
    /// iBatis数据上下文
    /// Powered By:luochengbang@163.com
    /// </summary>
    public class iBatisRepositoryContext : IRepositoryContext<ISqlMapper>
    {
        public iBatisRepositoryContext(System.IO.FileInfo resource)
        {
            DomSqlMapBuilder builder = new DomSqlMapBuilder();
            this.Context = builder.Configure(resource);
        }
        public iBatisRepositoryContext(Stream stream)
        {
            DomSqlMapBuilder builder = new DomSqlMapBuilder();
            this.Context = builder.Configure(stream);
        }

        public iBatisRepositoryContext(string resource)
        {
            DomSqlMapBuilder builder = new DomSqlMapBuilder();
            this.Context = builder.Configure(resource);
        }

        public iBatisRepositoryContext(XmlDocument document)
        {
            DomSqlMapBuilder builder = new DomSqlMapBuilder();
            this.Context = builder.Configure(document);
        }

        public iBatisRepositoryContext(Uri resource)
        {
            DomSqlMapBuilder builder = new DomSqlMapBuilder();
            this.Context = builder.Configure(resource);
        }

        /// <summary>
        /// ISqlMapper 操作数据库
        /// </summary>
        public virtual ISqlMapper Context
        {
            get; set;
        }


        private volatile bool committed = true;

        /// <summary>
        /// 事务
        /// </summary>
        public virtual DbTransaction DbTran
        {
            get
            {
                if (this.Conn != null)
                {
                    return this.Conn.BeginTransaction();
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// 数据连接对象
        /// </summary>
        public virtual DbConnection Conn { get; set; }


        #region IUnitOfWork Members
        /// <summary>
        /// 获取一个值，指示是否支持DTC
        /// </summary>
        public bool DTCompatible
        {
            // TODO: To be determined.
            get { return false; }
        }
        /// <summary>
        /// 获取一个 <see cref="System.Boolean"/> 值，指示是否成功提交事务
        /// </summary>
        public bool Committed
        {
            get { return this.committed; }
            protected set { committed = value; }
        }

        /// <summary>
        /// 提交事务
        /// </summary>
        public void Commit()
        {
            //if (!Committed)
            //{
            //    if (DbTran == null)
            //    {
            //        DbTran = conn.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted);
            //    }
            //    try
            //    {

            //        ctx.SaveChanges();
            //        if (enableReportIntercept)
            //        {
            //            foreach (object obj in reportingDataObjects)
            //            {
            //                Entity entity1 = obj as Entity;
            //                if (null != entity1)
            //                {
            //                    ReportingManager.HandleData(entity1);
            //                }
            //            }
            //        }
            //        dbTran.Commit();
            //        dbTran = null;
            //        //if (tran != null)
            //        //{
            //        //    tran.Complete();
            //        //    //tran.Dispose();
            //        //    //tran = null;
            //        //}
            //        Committed = true;
            //    }
            //    catch (DbEntityValidationException e)
            //    {
            //        if (dbTran != null)
            //        {
            //            dbTran.Rollback();
            //            dbTran = null;
            //        }
            //        foreach (var eve in e.EntityValidationErrors)
            //        {
            //            HzIns.BasicFramework.Common.Log.Logger.Error(
            //            string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
            //                eve.Entry.Entity.GetType().Name, eve.Entry.State), e);
            //            foreach (var ve in eve.ValidationErrors)
            //            {
            //                HzIns.BasicFramework.Common.Log.Logger.Error(
            //                string.Format("- Property: \"{0}\", Error: \"{1}\"",
            //                    ve.PropertyName, ve.ErrorMessage));
            //            }
            //        }
            //        throw;
            //    }
            //    //}
            //}

        }
        /// <summary>
        /// 回滚事物
        /// </summary>
        public void Rollback()
        {
        }

        #endregion

        public void Dispose()
        {
            //if (DbTran != null)
            //{
            //    DbTran.Rollback();
            //    DbTran = null;
            //}
            //if (Conn.State != System.Data.ConnectionState.Closed)
            //{
            //    Conn.Close();
            //    Conn = null;
            //}
        }
    }

}
